<template>
	<fui-form>
		<custom-form-item-card>
			<fui-form-item label="协作说明" label-width="auto">
				<template #vertical>
					<fui-textarea v-model="formData.assistRemark" :borderTop="false" placeholder="请输入"></fui-textarea>
				</template>
			</fui-form-item>
			<fui-form-item label="公共附件" label-width="auto" :bottom-border="false">
				<template #right>
					<custom-button-upload @change="changeCommonFile"></custom-button-upload>
					<!-- <custom-upload v-model="formData.assistEnclosure"></custom-upload> -->
				</template>
			</fui-form-item>
			<custom-file-list :list="fileList" @del="onDelFile"></custom-file-list>
		</custom-form-item-card>

		<custom-form-item-card :label="`协作人${index+1}`" v-for="(item,index) in formData.assistList">
			<template #other>
				<fui-icon v-if="index>0" name="delete" size="32" color="#0052D9" @click="del(index)"></fui-icon>
			</template>
			<fui-form-item label="协作人" label-width="auto" asterisk arrow>
				<template #right>
					<fui-text type="primary" :text=" item.sendLabel || '请选择'" :size="32"
						@click="choseUser(index)"></fui-text>
				</template>
			</fui-form-item>
			<fui-form-item label="协作事务" label-width="auto" asterisk :bottom-border="false">
				<template #vertical>
					<fui-textarea v-model="item.content" :borderTop="false" placeholder="请输入"></fui-textarea>
				</template>
			</fui-form-item>
			<fui-form-item label="附件" label-width="auto" :bottom-border="false">
				<template #right>
					<custom-button-upload @change="changeFilteItem($event,index)"></custom-button-upload>
				</template>
			</fui-form-item>
			<custom-file-list :list="item.assistEnclosure" @del="onDelItem($event,index)"></custom-file-list>
		</custom-form-item-card>

		<view class="m-t-32">
			<fui-button @click="onAdd">
				添加协作人
			</fui-button>
		</view>
	</fui-form>
</template>


<script>
	import {
		defineComponent
	} from "vue"
	import {
		mapState,
		mapWritableState,
		mapActions
	} from 'pinia'
	import {
		assistObj,
		useHandleWorkOrder_Cooperation
	} from "../../../../store/modules/handleWorkOrder_cooperation"
	import {
		base64ToFile,
		customValue,
		DeptSelectChoseType,
		toDeptSelectPage,
		toTeamUserSelectPage
	} from "../../../../utils/util"
	import {
		FormTeamUserStoreKey
	} from "../../../../store/sotreKey"
	import form from '@/components/firstui/fui-validator/fui-validator'
	import _ from 'lodash'
	import {
		WorkOrderMyIndexApi
	} from "../../../../api/workOrder"
	import {
		useTeamUserStore
	} from "../../../../store/modules/teamUser"
	import {
		FileApi
	} from "../../../../api/file"
	export default defineComponent({
		data() {
			return {}
		},
		computed: {
			...mapWritableState(useHandleWorkOrder_Cooperation, ['fileList', 'formData', 'choseUserIndex']),
			...mapState(useTeamUserStore, ['teamUserStoreKey']),
			user() {
				return this.teamUserStoreKey[FormTeamUserStoreKey.MyWorkOrderHandleCooperationUser.key]
			}
		},
		watch: {
			'user': {
				handler(val) {
					const type = {
						"team": 2,
						"user": 1
					}
					console.log(val);
					const {
						value
					} = val
					if (value.length) {
						const v = JSON.parse(value[0])
						this.formData.assistList[this.choseUserIndex] = {
							sendLabel: v.label,
							sendObjId: v.value,
							sendObjType: type[v.key],
							sendTeam: type[v.key] === 1 ? v.value : '',
							assistEnclosure: this.formData.assistList[this.choseUserIndex].assistEnclosure
						}
					} else {
						this.formData.assistList[this.choseUserIndex] = {
							sendLabel: '',
							sendObjId: null,
							sendObjType: null,
							sendTeam: '',
							assistEnclosure: this.formData.assistList[this.choseUserIndex].assistEnclosure
						}
					}

				},
				deep: true
			}
		},
		emits: ['disabled'],
		methods: {
			...mapActions(useHandleWorkOrder_Cooperation, ['onDelFile', 'onDelItem']),
			...mapActions(useTeamUserStore, ['clearTeamUserStoreKeyValue']),
			choseUser(index) {
				this.choseUserIndex = index
				toTeamUserSelectPage(FormTeamUserStoreKey.MyWorkOrderHandleCooperationUser.key)
			},
			changeFilteItem(f, index) {
				console.log(this.formData.assistList[index], 'this.formData.assistList[index]');
				const _f = this.formData.assistList[index].assistEnclosure

				this.formData.assistList[index].assistEnclosure = [..._f, ...f]
			},
			// 添加协作人
			onAdd() {
				this.formData.assistList.push(assistObj)

			},
			// 公共附件
			changeCommonFile(list) {

				this.fileList = [...this.fileList, ...list]
			},
			// 删除协作人
			del(index) {
				console.log(index);
				this.formData.assistList.splice(index, 1)
			},
			async submit(orderNum, cb) {
				console.log('submit');
				const rules = [{
					name: "sendObjType",
					rule: ["required"],
					msg: ["请选择协作人"],

				}, {
					name: "content",
					rule: ["required"],
					msg: ["请输入协作事务"],

				}];

				try {
					this.$emit('disabled', true)
					this.formData.orderNum = orderNum
					const the = this
					let isPassed = false
					let errorMsg = ''
					for (let i of this.formData.assistList) {
						const res = form.validator(i, rules)
						isPassed = res.isPassed
						errorMsg = res.errorMsg
					}
					if (this.fileList.length) {
						for (let i of this.fileList) {
							try {
								let f = base64ToFile(i.base64)
								const result = await FileApi.upload('image', f)
								const r = await FileApi.uploadWechatFile(result.mediaId)
								this.formData.assistEnclosure.push(r.data)
							} catch (err) {
								console.error(err);
							}

						}
					}
					for (let i of this.formData.assistList) {
						if (i.assistEnclosure.length) {
							const re = []
							for (let y of i.assistEnclosure) {
								try {
									let f = base64ToFile(y.base64)
									const result = await FileApi.upload('image', f)
									const r = await FileApi.uploadWechatFile(result.mediaId)
									re.push(r.data)
								} catch (err) {
									console.error(err);
								}

							}
							i.assistEnclosure = re
						}
					}
					console.log(this.formData.assistList, 'this.formData.assistList');
					if (isPassed) {
						await WorkOrderMyIndexApi.collaborativeWorkOrder(this.formData)
						this.fui.toast('提交成功')
						_.delay(() => {
							// the.formData = {
							// 	orderNum: '',
							// 	assistEnclosure: [],
							// 	assistRemark: '',
							// 	assistList: [assistObj],
							// }
							useHandleWorkOrder_Cooperation().$reset()
							this.clearTeamUserStoreKeyValue(FormTeamUserStoreKey
								.MyWorkOrderHandleCooperationUser.key, '')
							cb && cb()
						}, 1000)

					} else {
						this.$emit('disabled', false)
						this.fui.toast(errorMsg)
					}

				} catch (err) {
					this.$emit('disabled', false)
					console.error(err);
				}
			}

		},
	})
</script>

<style>
</style>